package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.ResultCode;
import com.atguigu.yygh.model.cmn.Dict;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    DictService dictService;

    /**
     * 根据Id查询其子类
     *
     * @param id：父Id
     * @return ：对应的子类
     */
    @GetMapping("/findChildData/{id}")
    @Cacheable(value = "dict", key = "'cache_'+#id")
    public ResultCode.R findChildData(@PathVariable Long id) {
        List<Dict> dictList = dictService.findChildData(id);
        return ResultCode.R.ok().data("list", dictList);
    }

    @GetMapping("getChildDataById/{id}")
    public List<Dict> getChildDataById(@PathVariable String id){
        List<Dict> dicts = dictService.getChildDataById(id);
        return dicts;
    }

    /**
     * 下载字典服务
     *
     * @param response :
     */
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    /**
     * 上传补充字典内容
     *
     * @param file :上传的文件
     */
    @PostMapping("/importData")
    @CacheEvict(value = "dict")
    public ResultCode.R importData(MultipartFile file) {
        dictService.importData(file);
        return ResultCode.R.ok();
    }

    /**
     * 医院等级查询
     * @param value
     * @param dictCode
     * @return
     */

    @GetMapping("/getName/{value}/{dictCode}")
    public String getNameByValueAndDictCode(@PathVariable String value,@PathVariable String dictCode){
        return dictService.getNameByValueAndDictCode(value,dictCode);
    }

    /**
     * 地址查询
     * @param value
     * @return
     */
    @GetMapping("/getName/{value}")
    public String getNameByValue(@PathVariable String value){
        String nameByValue = dictService.getNameByValue(value);
        return nameByValue;
    }

    ///admin/cmn/dict/getDict/{province}
    @GetMapping("getDict/{province}")
    public List<Dict> getDictByDictCode(@PathVariable String province){
        List<Dict> dicts = dictService.getDictByDictCode(province);
        return dicts;
    }

}
